


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SubSonic.DataProviders;
using SubSonic.Extensions;
using System.Linq.Expressions;
using SubSonic.Schema;
using System.Collections;
using SubSonic;
using SubSonic.Repository;
using System.ComponentModel;
using System.Data.Common;

namespace Eacademy.Domain.Entities
{
    
    
    /// <summary>
    /// A class which represents the acount_permission table in the Eacademy Database.
    /// </summary>
    public partial class AcountPermissionBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<AcountPermissionBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<AcountPermissionBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<AcountPermissionBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(AcountPermissionBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                AcountPermissionBase item=new AcountPermissionBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<AcountPermissionBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public AcountPermissionBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                AcountPermissionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AcountPermissionBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public AcountPermissionBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public AcountPermissionBase(Expression<Func<AcountPermissionBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<AcountPermissionBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<AcountPermissionBase> _repo;
            
            if(db.TestMode){
                AcountPermissionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AcountPermissionBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<AcountPermissionBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static AcountPermissionBase SingleOrDefault(Expression<Func<AcountPermissionBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            AcountPermissionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static AcountPermissionBase SingleOrDefault(Expression<Func<AcountPermissionBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            AcountPermissionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<AcountPermissionBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<AcountPermissionBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<AcountPermissionBase> Find(Expression<Func<AcountPermissionBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<AcountPermissionBase> Find(Expression<Func<AcountPermissionBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<AcountPermissionBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<AcountPermissionBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<AcountPermissionBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<AcountPermissionBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<AcountPermissionBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<AcountPermissionBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "AccountpermissionId";
        }

        public object KeyValue()
        {
            return this.AccountpermissionId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ModuleId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(AcountPermissionBase)){
                AcountPermissionBase compare=(AcountPermissionBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.AccountpermissionId;
        }
        
        public string DescriptorValue()
        {
                            return this.ModuleId.ToString();
                    }

        public string DescriptorColumn() {
            return "ModuleId";
        }
        public static string GetKeyColumn()
        {
            return "AccountpermissionId";
        }        
        public static string GetDescriptorColumn()
        {
            return "ModuleId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _AccountpermissionId;
        public int AccountpermissionId
        {
            get { return _AccountpermissionId; }
            set
            {
                if(_AccountpermissionId!=value){
                    _AccountpermissionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="accountpermission_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ModuleId;
        public int? ModuleId
        {
            get { return _ModuleId; }
            set
            {
                if(_ModuleId!=value){
                    _ModuleId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="module_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ModulePermissionId;
        public int? ModulePermissionId
        {
            get { return _ModulePermissionId; }
            set
            {
                if(_ModulePermissionId!=value){
                    _ModulePermissionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="module_permission_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<AcountPermissionBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the assessment table in the Eacademy Database.
    /// </summary>
    public partial class AssessmentBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<AssessmentBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<AssessmentBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<AssessmentBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(AssessmentBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                AssessmentBase item=new AssessmentBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<AssessmentBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public AssessmentBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                AssessmentBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssessmentBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public AssessmentBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public AssessmentBase(Expression<Func<AssessmentBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<AssessmentBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<AssessmentBase> _repo;
            
            if(db.TestMode){
                AssessmentBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssessmentBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<AssessmentBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static AssessmentBase SingleOrDefault(Expression<Func<AssessmentBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            AssessmentBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static AssessmentBase SingleOrDefault(Expression<Func<AssessmentBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            AssessmentBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<AssessmentBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<AssessmentBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<AssessmentBase> Find(Expression<Func<AssessmentBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<AssessmentBase> Find(Expression<Func<AssessmentBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<AssessmentBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<AssessmentBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<AssessmentBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<AssessmentBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<AssessmentBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<AssessmentBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "AssessmentId";
        }

        public object KeyValue()
        {
            return this.AssessmentId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Description.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(AssessmentBase)){
                AssessmentBase compare=(AssessmentBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.AssessmentId;
        }
        
        public string DescriptorValue()
        {
                            return this.Description.ToString();
                    }

        public string DescriptorColumn() {
            return "Description";
        }
        public static string GetKeyColumn()
        {
            return "AssessmentId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Description";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _AssessmentId;
        public int AssessmentId
        {
            get { return _AssessmentId; }
            set
            {
                if(_AssessmentId!=value){
                    _AssessmentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assessment_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Description;
        public string Description
        {
            get { return _Description; }
            set
            {
                if(_Description!=value){
                    _Description=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="description");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<AssessmentBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the assessment_questions table in the Eacademy Database.
    /// </summary>
    public partial class AssessmentQuestionBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<AssessmentQuestionBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<AssessmentQuestionBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<AssessmentQuestionBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(AssessmentQuestionBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                AssessmentQuestionBase item=new AssessmentQuestionBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<AssessmentQuestionBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public AssessmentQuestionBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                AssessmentQuestionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssessmentQuestionBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public AssessmentQuestionBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public AssessmentQuestionBase(Expression<Func<AssessmentQuestionBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<AssessmentQuestionBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<AssessmentQuestionBase> _repo;
            
            if(db.TestMode){
                AssessmentQuestionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssessmentQuestionBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<AssessmentQuestionBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static AssessmentQuestionBase SingleOrDefault(Expression<Func<AssessmentQuestionBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            AssessmentQuestionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static AssessmentQuestionBase SingleOrDefault(Expression<Func<AssessmentQuestionBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            AssessmentQuestionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<AssessmentQuestionBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<AssessmentQuestionBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<AssessmentQuestionBase> Find(Expression<Func<AssessmentQuestionBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<AssessmentQuestionBase> Find(Expression<Func<AssessmentQuestionBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<AssessmentQuestionBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<AssessmentQuestionBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<AssessmentQuestionBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<AssessmentQuestionBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<AssessmentQuestionBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<AssessmentQuestionBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "AssessmentQuestionId";
        }

        public object KeyValue()
        {
            return this.AssessmentQuestionId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Question.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(AssessmentQuestionBase)){
                AssessmentQuestionBase compare=(AssessmentQuestionBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.AssessmentQuestionId;
        }
        
        public string DescriptorValue()
        {
                            return this.Question.ToString();
                    }

        public string DescriptorColumn() {
            return "Question";
        }
        public static string GetKeyColumn()
        {
            return "AssessmentQuestionId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Question";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _AssessmentQuestionId;
        public int AssessmentQuestionId
        {
            get { return _AssessmentQuestionId; }
            set
            {
                if(_AssessmentQuestionId!=value){
                    _AssessmentQuestionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assessment_question_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _AssessmentId;
        public int AssessmentId
        {
            get { return _AssessmentId; }
            set
            {
                if(_AssessmentId!=value){
                    _AssessmentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assessment_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Question;
        public string Question
        {
            get { return _Question; }
            set
            {
                if(_Question!=value){
                    _Question=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="question");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<AssessmentQuestionBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the assessment_ratings table in the Eacademy Database.
    /// </summary>
    public partial class AssessmentRatingBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<AssessmentRatingBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<AssessmentRatingBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<AssessmentRatingBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(AssessmentRatingBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                AssessmentRatingBase item=new AssessmentRatingBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<AssessmentRatingBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public AssessmentRatingBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                AssessmentRatingBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssessmentRatingBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public AssessmentRatingBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public AssessmentRatingBase(Expression<Func<AssessmentRatingBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<AssessmentRatingBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<AssessmentRatingBase> _repo;
            
            if(db.TestMode){
                AssessmentRatingBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssessmentRatingBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<AssessmentRatingBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static AssessmentRatingBase SingleOrDefault(Expression<Func<AssessmentRatingBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            AssessmentRatingBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static AssessmentRatingBase SingleOrDefault(Expression<Func<AssessmentRatingBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            AssessmentRatingBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<AssessmentRatingBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<AssessmentRatingBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<AssessmentRatingBase> Find(Expression<Func<AssessmentRatingBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<AssessmentRatingBase> Find(Expression<Func<AssessmentRatingBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<AssessmentRatingBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<AssessmentRatingBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<AssessmentRatingBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<AssessmentRatingBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<AssessmentRatingBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<AssessmentRatingBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "AssessmentRatingId";
        }

        public object KeyValue()
        {
            return this.AssessmentRatingId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.AssessmentStudentId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(AssessmentRatingBase)){
                AssessmentRatingBase compare=(AssessmentRatingBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.AssessmentRatingId;
        }
        
        public string DescriptorValue()
        {
                            return this.AssessmentStudentId.ToString();
                    }

        public string DescriptorColumn() {
            return "AssessmentStudentId";
        }
        public static string GetKeyColumn()
        {
            return "AssessmentRatingId";
        }        
        public static string GetDescriptorColumn()
        {
            return "AssessmentStudentId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _AssessmentRatingId;
        public int AssessmentRatingId
        {
            get { return _AssessmentRatingId; }
            set
            {
                if(_AssessmentRatingId!=value){
                    _AssessmentRatingId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assessment_rating_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _AssessmentStudentId;
        public int AssessmentStudentId
        {
            get { return _AssessmentStudentId; }
            set
            {
                if(_AssessmentStudentId!=value){
                    _AssessmentStudentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assessment_student_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _AssessmentQuestionId;
        public int AssessmentQuestionId
        {
            get { return _AssessmentQuestionId; }
            set
            {
                if(_AssessmentQuestionId!=value){
                    _AssessmentQuestionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assessment_question_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Rating;
        public int Rating
        {
            get { return _Rating; }
            set
            {
                if(_Rating!=value){
                    _Rating=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="rating");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<AssessmentRatingBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the assessment_student table in the Eacademy Database.
    /// </summary>
    public partial class AssessmentStudentBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<AssessmentStudentBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<AssessmentStudentBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<AssessmentStudentBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(AssessmentStudentBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                AssessmentStudentBase item=new AssessmentStudentBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<AssessmentStudentBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public AssessmentStudentBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                AssessmentStudentBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssessmentStudentBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public AssessmentStudentBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public AssessmentStudentBase(Expression<Func<AssessmentStudentBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<AssessmentStudentBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<AssessmentStudentBase> _repo;
            
            if(db.TestMode){
                AssessmentStudentBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssessmentStudentBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<AssessmentStudentBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static AssessmentStudentBase SingleOrDefault(Expression<Func<AssessmentStudentBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            AssessmentStudentBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static AssessmentStudentBase SingleOrDefault(Expression<Func<AssessmentStudentBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            AssessmentStudentBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<AssessmentStudentBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<AssessmentStudentBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<AssessmentStudentBase> Find(Expression<Func<AssessmentStudentBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<AssessmentStudentBase> Find(Expression<Func<AssessmentStudentBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<AssessmentStudentBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<AssessmentStudentBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<AssessmentStudentBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<AssessmentStudentBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<AssessmentStudentBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<AssessmentStudentBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "AssessmentStudentId";
        }

        public object KeyValue()
        {
            return this.AssessmentStudentId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.AssessmentId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(AssessmentStudentBase)){
                AssessmentStudentBase compare=(AssessmentStudentBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.AssessmentStudentId;
        }
        
        public string DescriptorValue()
        {
                            return this.AssessmentId.ToString();
                    }

        public string DescriptorColumn() {
            return "AssessmentId";
        }
        public static string GetKeyColumn()
        {
            return "AssessmentStudentId";
        }        
        public static string GetDescriptorColumn()
        {
            return "AssessmentId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _AssessmentStudentId;
        public int AssessmentStudentId
        {
            get { return _AssessmentStudentId; }
            set
            {
                if(_AssessmentStudentId!=value){
                    _AssessmentStudentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assessment_student_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _AssessmentId;
        public int AssessmentId
        {
            get { return _AssessmentId; }
            set
            {
                if(_AssessmentId!=value){
                    _AssessmentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assessment_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _StudentId;
        public int StudentId
        {
            get { return _StudentId; }
            set
            {
                if(_StudentId!=value){
                    _StudentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="student_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _MentorId;
        public int MentorId
        {
            get { return _MentorId; }
            set
            {
                if(_MentorId!=value){
                    _MentorId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="mentor_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _SubjectId;
        public int SubjectId
        {
            get { return _SubjectId; }
            set
            {
                if(_SubjectId!=value){
                    _SubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Score;
        public int Score
        {
            get { return _Score; }
            set
            {
                if(_Score!=value){
                    _Score=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="score");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<AssessmentStudentBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the assignments table in the Eacademy Database.
    /// </summary>
    public partial class AssignmentBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<AssignmentBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<AssignmentBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<AssignmentBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(AssignmentBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                AssignmentBase item=new AssignmentBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<AssignmentBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public AssignmentBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                AssignmentBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssignmentBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public AssignmentBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public AssignmentBase(Expression<Func<AssignmentBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<AssignmentBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<AssignmentBase> _repo;
            
            if(db.TestMode){
                AssignmentBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssignmentBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<AssignmentBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static AssignmentBase SingleOrDefault(Expression<Func<AssignmentBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            AssignmentBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static AssignmentBase SingleOrDefault(Expression<Func<AssignmentBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            AssignmentBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<AssignmentBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<AssignmentBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<AssignmentBase> Find(Expression<Func<AssignmentBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<AssignmentBase> Find(Expression<Func<AssignmentBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<AssignmentBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<AssignmentBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<AssignmentBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<AssignmentBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<AssignmentBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<AssignmentBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "AssignmentId";
        }

        public object KeyValue()
        {
            return this.AssignmentId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Title.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(AssignmentBase)){
                AssignmentBase compare=(AssignmentBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.AssignmentId;
        }
        
        public string DescriptorValue()
        {
                            return this.Title.ToString();
                    }

        public string DescriptorColumn() {
            return "Title";
        }
        public static string GetKeyColumn()
        {
            return "AssignmentId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Title";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _AssignmentId;
        public int AssignmentId
        {
            get { return _AssignmentId; }
            set
            {
                if(_AssignmentId!=value){
                    _AssignmentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assignment_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Title;
        public string Title
        {
            get { return _Title; }
            set
            {
                if(_Title!=value){
                    _Title=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="title");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Assignment;
        public string Assignment
        {
            get { return _Assignment; }
            set
            {
                if(_Assignment!=value){
                    _Assignment=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assignment");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime _DateDue;
        public DateTime DateDue
        {
            get { return _DateDue; }
            set
            {
                if(_DateDue!=value){
                    _DateDue=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="date_due");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime _DatePosted;
        public DateTime DatePosted
        {
            get { return _DatePosted; }
            set
            {
                if(_DatePosted!=value){
                    _DatePosted=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="date_posted");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime _DateModified;
        public DateTime DateModified
        {
            get { return _DateModified; }
            set
            {
                if(_DateModified!=value){
                    _DateModified=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="date_modified");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Files;
        public string Files
        {
            get { return _Files; }
            set
            {
                if(_Files!=value){
                    _Files=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="files");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<AssignmentBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the assignments_subjects table in the Eacademy Database.
    /// </summary>
    public partial class AssignmentsSubjectBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<AssignmentsSubjectBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<AssignmentsSubjectBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<AssignmentsSubjectBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(AssignmentsSubjectBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                AssignmentsSubjectBase item=new AssignmentsSubjectBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<AssignmentsSubjectBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public AssignmentsSubjectBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                AssignmentsSubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssignmentsSubjectBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public AssignmentsSubjectBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public AssignmentsSubjectBase(Expression<Func<AssignmentsSubjectBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<AssignmentsSubjectBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<AssignmentsSubjectBase> _repo;
            
            if(db.TestMode){
                AssignmentsSubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<AssignmentsSubjectBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<AssignmentsSubjectBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static AssignmentsSubjectBase SingleOrDefault(Expression<Func<AssignmentsSubjectBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            AssignmentsSubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static AssignmentsSubjectBase SingleOrDefault(Expression<Func<AssignmentsSubjectBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            AssignmentsSubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<AssignmentsSubjectBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<AssignmentsSubjectBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<AssignmentsSubjectBase> Find(Expression<Func<AssignmentsSubjectBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<AssignmentsSubjectBase> Find(Expression<Func<AssignmentsSubjectBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<AssignmentsSubjectBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<AssignmentsSubjectBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<AssignmentsSubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<AssignmentsSubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<AssignmentsSubjectBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<AssignmentsSubjectBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "AssignmentsSubjectId";
        }

        public object KeyValue()
        {
            return this.AssignmentsSubjectId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.AssignmentId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(AssignmentsSubjectBase)){
                AssignmentsSubjectBase compare=(AssignmentsSubjectBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.AssignmentsSubjectId;
        }
        
        public string DescriptorValue()
        {
                            return this.AssignmentId.ToString();
                    }

        public string DescriptorColumn() {
            return "AssignmentId";
        }
        public static string GetKeyColumn()
        {
            return "AssignmentsSubjectId";
        }        
        public static string GetDescriptorColumn()
        {
            return "AssignmentId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _AssignmentsSubjectId;
        public int AssignmentsSubjectId
        {
            get { return _AssignmentsSubjectId; }
            set
            {
                if(_AssignmentsSubjectId!=value){
                    _AssignmentsSubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assignments_subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _AssignmentId;
        public int? AssignmentId
        {
            get { return _AssignmentId; }
            set
            {
                if(_AssignmentId!=value){
                    _AssignmentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="assignment_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _SubjectId;
        public int? SubjectId
        {
            get { return _SubjectId; }
            set
            {
                if(_SubjectId!=value){
                    _SubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _MentorId;
        public int MentorId
        {
            get { return _MentorId; }
            set
            {
                if(_MentorId!=value){
                    _MentorId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="mentor_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<AssignmentsSubjectBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the choices table in the Eacademy Database.
    /// </summary>
    public partial class ChoiceBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ChoiceBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ChoiceBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ChoiceBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ChoiceBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ChoiceBase item=new ChoiceBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ChoiceBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public ChoiceBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ChoiceBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ChoiceBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ChoiceBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ChoiceBase(Expression<Func<ChoiceBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ChoiceBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<ChoiceBase> _repo;
            
            if(db.TestMode){
                ChoiceBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ChoiceBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ChoiceBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static ChoiceBase SingleOrDefault(Expression<Func<ChoiceBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ChoiceBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ChoiceBase SingleOrDefault(Expression<Func<ChoiceBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ChoiceBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ChoiceBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ChoiceBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ChoiceBase> Find(Expression<Func<ChoiceBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ChoiceBase> Find(Expression<Func<ChoiceBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ChoiceBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ChoiceBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<ChoiceBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<ChoiceBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<ChoiceBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<ChoiceBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ChoiceId";
        }

        public object KeyValue()
        {
            return this.ChoiceId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Choice.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ChoiceBase)){
                ChoiceBase compare=(ChoiceBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ChoiceId;
        }
        
        public string DescriptorValue()
        {
                            return this.Choice.ToString();
                    }

        public string DescriptorColumn() {
            return "Choice";
        }
        public static string GetKeyColumn()
        {
            return "ChoiceId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Choice";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ChoiceId;
        public int ChoiceId
        {
            get { return _ChoiceId; }
            set
            {
                if(_ChoiceId!=value){
                    _ChoiceId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="choice_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _QuestionId;
        public int QuestionId
        {
            get { return _QuestionId; }
            set
            {
                if(_QuestionId!=value){
                    _QuestionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="question_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Choice;
        public string Choice
        {
            get { return _Choice; }
            set
            {
                if(_Choice!=value){
                    _Choice=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="choice");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        bool _IsAnswer;
        public bool IsAnswer
        {
            get { return _IsAnswer; }
            set
            {
                if(_IsAnswer!=value){
                    _IsAnswer=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="is_answer");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ChoiceBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the cometchat table in the Eacademy Database.
    /// </summary>
    public partial class CometchatBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<CometchatBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<CometchatBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<CometchatBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(CometchatBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                CometchatBase item=new CometchatBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<CometchatBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public CometchatBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                CometchatBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public CometchatBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public CometchatBase(Expression<Func<CometchatBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<CometchatBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<CometchatBase> _repo;
            
            if(db.TestMode){
                CometchatBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<CometchatBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static CometchatBase SingleOrDefault(Expression<Func<CometchatBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            CometchatBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static CometchatBase SingleOrDefault(Expression<Func<CometchatBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            CometchatBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<CometchatBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<CometchatBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<CometchatBase> Find(Expression<Func<CometchatBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<CometchatBase> Find(Expression<Func<CometchatBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<CometchatBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<CometchatBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<CometchatBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<CometchatBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<CometchatBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<CometchatBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Message.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(CometchatBase)){
                CometchatBase compare=(CometchatBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.Message.ToString();
                    }

        public string DescriptorColumn() {
            return "Message";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Message";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _From;
        public int From
        {
            get { return _From; }
            set
            {
                if(_From!=value){
                    _From=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="from");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _To;
        public int To
        {
            get { return _To; }
            set
            {
                if(_To!=value){
                    _To=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="to");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Message;
        public string Message
        {
            get { return _Message; }
            set
            {
                if(_Message!=value){
                    _Message=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="message");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Sent;
        public int Sent
        {
            get { return _Sent; }
            set
            {
                if(_Sent!=value){
                    _Sent=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="sent");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Read;
        public int Read
        {
            get { return _Read; }
            set
            {
                if(_Read!=value){
                    _Read=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="read");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Direction;
        public int Direction
        {
            get { return _Direction; }
            set
            {
                if(_Direction!=value){
                    _Direction=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="direction");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<CometchatBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the cometchat_announcements table in the Eacademy Database.
    /// </summary>
    public partial class CometchatAnnouncementBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<CometchatAnnouncementBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<CometchatAnnouncementBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<CometchatAnnouncementBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(CometchatAnnouncementBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                CometchatAnnouncementBase item=new CometchatAnnouncementBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<CometchatAnnouncementBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public CometchatAnnouncementBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                CometchatAnnouncementBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatAnnouncementBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public CometchatAnnouncementBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public CometchatAnnouncementBase(Expression<Func<CometchatAnnouncementBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<CometchatAnnouncementBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<CometchatAnnouncementBase> _repo;
            
            if(db.TestMode){
                CometchatAnnouncementBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatAnnouncementBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<CometchatAnnouncementBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static CometchatAnnouncementBase SingleOrDefault(Expression<Func<CometchatAnnouncementBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            CometchatAnnouncementBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static CometchatAnnouncementBase SingleOrDefault(Expression<Func<CometchatAnnouncementBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            CometchatAnnouncementBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<CometchatAnnouncementBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<CometchatAnnouncementBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<CometchatAnnouncementBase> Find(Expression<Func<CometchatAnnouncementBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<CometchatAnnouncementBase> Find(Expression<Func<CometchatAnnouncementBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<CometchatAnnouncementBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<CometchatAnnouncementBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<CometchatAnnouncementBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<CometchatAnnouncementBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<CometchatAnnouncementBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<CometchatAnnouncementBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Announcement.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(CometchatAnnouncementBase)){
                CometchatAnnouncementBase compare=(CometchatAnnouncementBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.Announcement.ToString();
                    }

        public string DescriptorColumn() {
            return "Announcement";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Announcement";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Announcement;
        public string Announcement
        {
            get { return _Announcement; }
            set
            {
                if(_Announcement!=value){
                    _Announcement=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="announcement");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Time;
        public int Time
        {
            get { return _Time; }
            set
            {
                if(_Time!=value){
                    _Time=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="time");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _To;
        public int To
        {
            get { return _To; }
            set
            {
                if(_To!=value){
                    _To=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="to");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<CometchatAnnouncementBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the cometchat_chatroommessages table in the Eacademy Database.
    /// </summary>
    public partial class CometchatChatroommessageBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<CometchatChatroommessageBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<CometchatChatroommessageBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<CometchatChatroommessageBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(CometchatChatroommessageBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                CometchatChatroommessageBase item=new CometchatChatroommessageBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<CometchatChatroommessageBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public CometchatChatroommessageBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                CometchatChatroommessageBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatChatroommessageBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public CometchatChatroommessageBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public CometchatChatroommessageBase(Expression<Func<CometchatChatroommessageBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<CometchatChatroommessageBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<CometchatChatroommessageBase> _repo;
            
            if(db.TestMode){
                CometchatChatroommessageBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatChatroommessageBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<CometchatChatroommessageBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static CometchatChatroommessageBase SingleOrDefault(Expression<Func<CometchatChatroommessageBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            CometchatChatroommessageBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static CometchatChatroommessageBase SingleOrDefault(Expression<Func<CometchatChatroommessageBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            CometchatChatroommessageBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<CometchatChatroommessageBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<CometchatChatroommessageBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<CometchatChatroommessageBase> Find(Expression<Func<CometchatChatroommessageBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<CometchatChatroommessageBase> Find(Expression<Func<CometchatChatroommessageBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<CometchatChatroommessageBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<CometchatChatroommessageBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<CometchatChatroommessageBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<CometchatChatroommessageBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<CometchatChatroommessageBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<CometchatChatroommessageBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Message.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(CometchatChatroommessageBase)){
                CometchatChatroommessageBase compare=(CometchatChatroommessageBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.Message.ToString();
                    }

        public string DescriptorColumn() {
            return "Message";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Message";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Userid;
        public int Userid
        {
            get { return _Userid; }
            set
            {
                if(_Userid!=value){
                    _Userid=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="userid");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Chatroomid;
        public int Chatroomid
        {
            get { return _Chatroomid; }
            set
            {
                if(_Chatroomid!=value){
                    _Chatroomid=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="chatroomid");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Message;
        public string Message
        {
            get { return _Message; }
            set
            {
                if(_Message!=value){
                    _Message=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="message");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Sent;
        public int Sent
        {
            get { return _Sent; }
            set
            {
                if(_Sent!=value){
                    _Sent=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="sent");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<CometchatChatroommessageBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the cometchat_chatrooms table in the Eacademy Database.
    /// </summary>
    public partial class CometchatChatroomBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<CometchatChatroomBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<CometchatChatroomBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<CometchatChatroomBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(CometchatChatroomBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                CometchatChatroomBase item=new CometchatChatroomBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<CometchatChatroomBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public CometchatChatroomBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                CometchatChatroomBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatChatroomBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public CometchatChatroomBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public CometchatChatroomBase(Expression<Func<CometchatChatroomBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<CometchatChatroomBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<CometchatChatroomBase> _repo;
            
            if(db.TestMode){
                CometchatChatroomBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatChatroomBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<CometchatChatroomBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static CometchatChatroomBase SingleOrDefault(Expression<Func<CometchatChatroomBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            CometchatChatroomBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static CometchatChatroomBase SingleOrDefault(Expression<Func<CometchatChatroomBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            CometchatChatroomBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<CometchatChatroomBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<CometchatChatroomBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<CometchatChatroomBase> Find(Expression<Func<CometchatChatroomBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<CometchatChatroomBase> Find(Expression<Func<CometchatChatroomBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<CometchatChatroomBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<CometchatChatroomBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<CometchatChatroomBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<CometchatChatroomBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<CometchatChatroomBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<CometchatChatroomBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Name.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(CometchatChatroomBase)){
                CometchatChatroomBase compare=(CometchatChatroomBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.Name.ToString();
                    }

        public string DescriptorColumn() {
            return "Name";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Name";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if(_Name!=value){
                    _Name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Lastactivity;
        public int Lastactivity
        {
            get { return _Lastactivity; }
            set
            {
                if(_Lastactivity!=value){
                    _Lastactivity=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="lastactivity");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Createdby;
        public int Createdby
        {
            get { return _Createdby; }
            set
            {
                if(_Createdby!=value){
                    _Createdby=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="createdby");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Password;
        public string Password
        {
            get { return _Password; }
            set
            {
                if(_Password!=value){
                    _Password=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="password");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Type;
        public int Type
        {
            get { return _Type; }
            set
            {
                if(_Type!=value){
                    _Type=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="type");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<CometchatChatroomBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the cometchat_chatrooms_users table in the Eacademy Database.
    /// </summary>
    public partial class CometchatChatroomsUserBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<CometchatChatroomsUserBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<CometchatChatroomsUserBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<CometchatChatroomsUserBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(CometchatChatroomsUserBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                CometchatChatroomsUserBase item=new CometchatChatroomsUserBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<CometchatChatroomsUserBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public CometchatChatroomsUserBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                CometchatChatroomsUserBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatChatroomsUserBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public CometchatChatroomsUserBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public CometchatChatroomsUserBase(Expression<Func<CometchatChatroomsUserBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<CometchatChatroomsUserBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<CometchatChatroomsUserBase> _repo;
            
            if(db.TestMode){
                CometchatChatroomsUserBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatChatroomsUserBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<CometchatChatroomsUserBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static CometchatChatroomsUserBase SingleOrDefault(Expression<Func<CometchatChatroomsUserBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            CometchatChatroomsUserBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static CometchatChatroomsUserBase SingleOrDefault(Expression<Func<CometchatChatroomsUserBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            CometchatChatroomsUserBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<CometchatChatroomsUserBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<CometchatChatroomsUserBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<CometchatChatroomsUserBase> Find(Expression<Func<CometchatChatroomsUserBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<CometchatChatroomsUserBase> Find(Expression<Func<CometchatChatroomsUserBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<CometchatChatroomsUserBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<CometchatChatroomsUserBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<CometchatChatroomsUserBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<CometchatChatroomsUserBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<CometchatChatroomsUserBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<CometchatChatroomsUserBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Userid";
        }

        public object KeyValue()
        {
            return this.Userid;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Chatroomid.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(CometchatChatroomsUserBase)){
                CometchatChatroomsUserBase compare=(CometchatChatroomsUserBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Userid;
        }
        
        public string DescriptorValue()
        {
                            return this.Chatroomid.ToString();
                    }

        public string DescriptorColumn() {
            return "Chatroomid";
        }
        public static string GetKeyColumn()
        {
            return "Userid";
        }        
        public static string GetDescriptorColumn()
        {
            return "Chatroomid";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Userid;
        public int Userid
        {
            get { return _Userid; }
            set
            {
                if(_Userid!=value){
                    _Userid=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="userid");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Chatroomid;
        public int Chatroomid
        {
            get { return _Chatroomid; }
            set
            {
                if(_Chatroomid!=value){
                    _Chatroomid=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="chatroomid");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Lastactivity;
        public int Lastactivity
        {
            get { return _Lastactivity; }
            set
            {
                if(_Lastactivity!=value){
                    _Lastactivity=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="lastactivity");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<CometchatChatroomsUserBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the cometchat_messages_old table in the Eacademy Database.
    /// </summary>
    public partial class CometchatMessagesOldBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<CometchatMessagesOldBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<CometchatMessagesOldBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<CometchatMessagesOldBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(CometchatMessagesOldBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                CometchatMessagesOldBase item=new CometchatMessagesOldBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<CometchatMessagesOldBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public CometchatMessagesOldBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                CometchatMessagesOldBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatMessagesOldBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public CometchatMessagesOldBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public CometchatMessagesOldBase(Expression<Func<CometchatMessagesOldBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<CometchatMessagesOldBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<CometchatMessagesOldBase> _repo;
            
            if(db.TestMode){
                CometchatMessagesOldBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatMessagesOldBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<CometchatMessagesOldBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static CometchatMessagesOldBase SingleOrDefault(Expression<Func<CometchatMessagesOldBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            CometchatMessagesOldBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static CometchatMessagesOldBase SingleOrDefault(Expression<Func<CometchatMessagesOldBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            CometchatMessagesOldBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<CometchatMessagesOldBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<CometchatMessagesOldBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<CometchatMessagesOldBase> Find(Expression<Func<CometchatMessagesOldBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<CometchatMessagesOldBase> Find(Expression<Func<CometchatMessagesOldBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<CometchatMessagesOldBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<CometchatMessagesOldBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<CometchatMessagesOldBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<CometchatMessagesOldBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<CometchatMessagesOldBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<CometchatMessagesOldBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Message.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(CometchatMessagesOldBase)){
                CometchatMessagesOldBase compare=(CometchatMessagesOldBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.Message.ToString();
                    }

        public string DescriptorColumn() {
            return "Message";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Message";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _From;
        public int From
        {
            get { return _From; }
            set
            {
                if(_From!=value){
                    _From=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="from");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _To;
        public int To
        {
            get { return _To; }
            set
            {
                if(_To!=value){
                    _To=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="to");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Message;
        public string Message
        {
            get { return _Message; }
            set
            {
                if(_Message!=value){
                    _Message=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="message");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Sent;
        public int Sent
        {
            get { return _Sent; }
            set
            {
                if(_Sent!=value){
                    _Sent=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="sent");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Read;
        public int Read
        {
            get { return _Read; }
            set
            {
                if(_Read!=value){
                    _Read=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="read");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Direction;
        public int Direction
        {
            get { return _Direction; }
            set
            {
                if(_Direction!=value){
                    _Direction=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="direction");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<CometchatMessagesOldBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the cometchat_status table in the Eacademy Database.
    /// </summary>
    public partial class CometchatStatusBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<CometchatStatusBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<CometchatStatusBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<CometchatStatusBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(CometchatStatusBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                CometchatStatusBase item=new CometchatStatusBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<CometchatStatusBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public CometchatStatusBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                CometchatStatusBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatStatusBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public CometchatStatusBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public CometchatStatusBase(Expression<Func<CometchatStatusBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<CometchatStatusBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<CometchatStatusBase> _repo;
            
            if(db.TestMode){
                CometchatStatusBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatStatusBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<CometchatStatusBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static CometchatStatusBase SingleOrDefault(Expression<Func<CometchatStatusBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            CometchatStatusBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static CometchatStatusBase SingleOrDefault(Expression<Func<CometchatStatusBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            CometchatStatusBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<CometchatStatusBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<CometchatStatusBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<CometchatStatusBase> Find(Expression<Func<CometchatStatusBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<CometchatStatusBase> Find(Expression<Func<CometchatStatusBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<CometchatStatusBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<CometchatStatusBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<CometchatStatusBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<CometchatStatusBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<CometchatStatusBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<CometchatStatusBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Userid";
        }

        public object KeyValue()
        {
            return this.Userid;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Message.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(CometchatStatusBase)){
                CometchatStatusBase compare=(CometchatStatusBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Userid;
        }
        
        public string DescriptorValue()
        {
                            return this.Message.ToString();
                    }

        public string DescriptorColumn() {
            return "Message";
        }
        public static string GetKeyColumn()
        {
            return "Userid";
        }        
        public static string GetDescriptorColumn()
        {
            return "Message";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Userid;
        public int Userid
        {
            get { return _Userid; }
            set
            {
                if(_Userid!=value){
                    _Userid=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="userid");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Message;
        public string Message
        {
            get { return _Message; }
            set
            {
                if(_Message!=value){
                    _Message=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="message");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Status;
        public string Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Typingto;
        public int? Typingto
        {
            get { return _Typingto; }
            set
            {
                if(_Typingto!=value){
                    _Typingto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="typingto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Typingtime;
        public int? Typingtime
        {
            get { return _Typingtime; }
            set
            {
                if(_Typingtime!=value){
                    _Typingtime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="typingtime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<CometchatStatusBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the cometchat_videochatsessions table in the Eacademy Database.
    /// </summary>
    public partial class CometchatVideochatsessionBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<CometchatVideochatsessionBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<CometchatVideochatsessionBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<CometchatVideochatsessionBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(CometchatVideochatsessionBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                CometchatVideochatsessionBase item=new CometchatVideochatsessionBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<CometchatVideochatsessionBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public CometchatVideochatsessionBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                CometchatVideochatsessionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatVideochatsessionBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public CometchatVideochatsessionBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public CometchatVideochatsessionBase(Expression<Func<CometchatVideochatsessionBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<CometchatVideochatsessionBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<CometchatVideochatsessionBase> _repo;
            
            if(db.TestMode){
                CometchatVideochatsessionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<CometchatVideochatsessionBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<CometchatVideochatsessionBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static CometchatVideochatsessionBase SingleOrDefault(Expression<Func<CometchatVideochatsessionBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            CometchatVideochatsessionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static CometchatVideochatsessionBase SingleOrDefault(Expression<Func<CometchatVideochatsessionBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            CometchatVideochatsessionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<CometchatVideochatsessionBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<CometchatVideochatsessionBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<CometchatVideochatsessionBase> Find(Expression<Func<CometchatVideochatsessionBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<CometchatVideochatsessionBase> Find(Expression<Func<CometchatVideochatsessionBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<CometchatVideochatsessionBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<CometchatVideochatsessionBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<CometchatVideochatsessionBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<CometchatVideochatsessionBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<CometchatVideochatsessionBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<CometchatVideochatsessionBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Username";
        }

        public object KeyValue()
        {
            return this.Username;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<string>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Username.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(CometchatVideochatsessionBase)){
                CometchatVideochatsessionBase compare=(CometchatVideochatsessionBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public string DescriptorValue()
        {
                            return this.Username.ToString();
                    }

        public string DescriptorColumn() {
            return "Username";
        }
        public static string GetKeyColumn()
        {
            return "Username";
        }        
        public static string GetDescriptorColumn()
        {
            return "Username";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        string _Username;
        public string Username
        {
            get { return _Username; }
            set
            {
                if(_Username!=value){
                    _Username=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="username");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Identity;
        public string Identity
        {
            get { return _Identity; }
            set
            {
                if(_Identity!=value){
                    _Identity=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="identity");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Timestamp;
        public int? Timestamp
        {
            get { return _Timestamp; }
            set
            {
                if(_Timestamp!=value){
                    _Timestamp=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="timestamp");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<CometchatVideochatsessionBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the elmah_error table in the Eacademy Database.
    /// </summary>
    public partial class ElmahErrorBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ElmahErrorBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ElmahErrorBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ElmahErrorBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ElmahErrorBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ElmahErrorBase item=new ElmahErrorBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ElmahErrorBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public ElmahErrorBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ElmahErrorBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ElmahErrorBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ElmahErrorBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ElmahErrorBase(Expression<Func<ElmahErrorBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ElmahErrorBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<ElmahErrorBase> _repo;
            
            if(db.TestMode){
                ElmahErrorBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ElmahErrorBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ElmahErrorBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static ElmahErrorBase SingleOrDefault(Expression<Func<ElmahErrorBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ElmahErrorBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ElmahErrorBase SingleOrDefault(Expression<Func<ElmahErrorBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ElmahErrorBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ElmahErrorBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ElmahErrorBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ElmahErrorBase> Find(Expression<Func<ElmahErrorBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ElmahErrorBase> Find(Expression<Func<ElmahErrorBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ElmahErrorBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ElmahErrorBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<ElmahErrorBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<ElmahErrorBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<ElmahErrorBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<ElmahErrorBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Sequence";
        }

        public object KeyValue()
        {
            return this.Sequence;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ErrorId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ElmahErrorBase)){
                ElmahErrorBase compare=(ElmahErrorBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Sequence;
        }
        
        public string DescriptorValue()
        {
                            return this.ErrorId.ToString();
                    }

        public string DescriptorColumn() {
            return "ErrorId";
        }
        public static string GetKeyColumn()
        {
            return "Sequence";
        }        
        public static string GetDescriptorColumn()
        {
            return "ErrorId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        string _ErrorId;
        public string ErrorId
        {
            get { return _ErrorId; }
            set
            {
                if(_ErrorId!=value){
                    _ErrorId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ErrorId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Application;
        public string Application
        {
            get { return _Application; }
            set
            {
                if(_Application!=value){
                    _Application=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Application");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Host;
        public string Host
        {
            get { return _Host; }
            set
            {
                if(_Host!=value){
                    _Host=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Host");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Type;
        public string Type
        {
            get { return _Type; }
            set
            {
                if(_Type!=value){
                    _Type=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Type");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Source;
        public string Source
        {
            get { return _Source; }
            set
            {
                if(_Source!=value){
                    _Source=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Source");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Message;
        public string Message
        {
            get { return _Message; }
            set
            {
                if(_Message!=value){
                    _Message=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Message");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _User;
        public string User
        {
            get { return _User; }
            set
            {
                if(_User!=value){
                    _User=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="User");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _StatusCode;
        public int StatusCode
        {
            get { return _StatusCode; }
            set
            {
                if(_StatusCode!=value){
                    _StatusCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="StatusCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime _TimeUtc;
        public DateTime TimeUtc
        {
            get { return _TimeUtc; }
            set
            {
                if(_TimeUtc!=value){
                    _TimeUtc=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TimeUtc");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Sequence;
        public int Sequence
        {
            get { return _Sequence; }
            set
            {
                if(_Sequence!=value){
                    _Sequence=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Sequence");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _AllXml;
        public string AllXml
        {
            get { return _AllXml; }
            set
            {
                if(_AllXml!=value){
                    _AllXml=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AllXml");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ElmahErrorBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the friends table in the Eacademy Database.
    /// </summary>
    public partial class FriendBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<FriendBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<FriendBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<FriendBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(FriendBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                FriendBase item=new FriendBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<FriendBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public FriendBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                FriendBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<FriendBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public FriendBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public FriendBase(Expression<Func<FriendBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<FriendBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<FriendBase> _repo;
            
            if(db.TestMode){
                FriendBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<FriendBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<FriendBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static FriendBase SingleOrDefault(Expression<Func<FriendBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            FriendBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static FriendBase SingleOrDefault(Expression<Func<FriendBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            FriendBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<FriendBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<FriendBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<FriendBase> Find(Expression<Func<FriendBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<FriendBase> Find(Expression<Func<FriendBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<FriendBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<FriendBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<FriendBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<FriendBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<FriendBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<FriendBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Toid.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(FriendBase)){
                FriendBase compare=(FriendBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.Toid.ToString();
                    }

        public string DescriptorColumn() {
            return "Toid";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Toid";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Toid;
        public int? Toid
        {
            get { return _Toid; }
            set
            {
                if(_Toid!=value){
                    _Toid=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="toid");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Fromid;
        public int? Fromid
        {
            get { return _Fromid; }
            set
            {
                if(_Fromid!=value){
                    _Fromid=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="fromid");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<FriendBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the glossary table in the Eacademy Database.
    /// </summary>
    public partial class GlossaryBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<GlossaryBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<GlossaryBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<GlossaryBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(GlossaryBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                GlossaryBase item=new GlossaryBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<GlossaryBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public GlossaryBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                GlossaryBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<GlossaryBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public GlossaryBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public GlossaryBase(Expression<Func<GlossaryBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<GlossaryBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<GlossaryBase> _repo;
            
            if(db.TestMode){
                GlossaryBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<GlossaryBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<GlossaryBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static GlossaryBase SingleOrDefault(Expression<Func<GlossaryBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            GlossaryBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static GlossaryBase SingleOrDefault(Expression<Func<GlossaryBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            GlossaryBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<GlossaryBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<GlossaryBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<GlossaryBase> Find(Expression<Func<GlossaryBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<GlossaryBase> Find(Expression<Func<GlossaryBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<GlossaryBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<GlossaryBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<GlossaryBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<GlossaryBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<GlossaryBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<GlossaryBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "TermId";
        }

        public object KeyValue()
        {
            return this.TermId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Term.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(GlossaryBase)){
                GlossaryBase compare=(GlossaryBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.TermId;
        }
        
        public string DescriptorValue()
        {
                            return this.Term.ToString();
                    }

        public string DescriptorColumn() {
            return "Term";
        }
        public static string GetKeyColumn()
        {
            return "TermId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Term";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _TermId;
        public int TermId
        {
            get { return _TermId; }
            set
            {
                if(_TermId!=value){
                    _TermId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="term_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Term;
        public string Term
        {
            get { return _Term; }
            set
            {
                if(_Term!=value){
                    _Term=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="term");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Definition;
        public string Definition
        {
            get { return _Definition; }
            set
            {
                if(_Definition!=value){
                    _Definition=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="definition");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<GlossaryBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the lessons table in the Eacademy Database.
    /// </summary>
    public partial class LessonBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<LessonBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<LessonBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<LessonBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(LessonBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                LessonBase item=new LessonBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<LessonBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public LessonBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                LessonBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<LessonBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public LessonBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public LessonBase(Expression<Func<LessonBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<LessonBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<LessonBase> _repo;
            
            if(db.TestMode){
                LessonBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<LessonBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<LessonBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static LessonBase SingleOrDefault(Expression<Func<LessonBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            LessonBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static LessonBase SingleOrDefault(Expression<Func<LessonBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            LessonBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<LessonBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<LessonBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<LessonBase> Find(Expression<Func<LessonBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<LessonBase> Find(Expression<Func<LessonBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<LessonBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<LessonBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<LessonBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<LessonBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<LessonBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<LessonBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "LessonId";
        }

        public object KeyValue()
        {
            return this.LessonId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Title.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(LessonBase)){
                LessonBase compare=(LessonBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.LessonId;
        }
        
        public string DescriptorValue()
        {
                            return this.Title.ToString();
                    }

        public string DescriptorColumn() {
            return "Title";
        }
        public static string GetKeyColumn()
        {
            return "LessonId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Title";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _LessonId;
        public int LessonId
        {
            get { return _LessonId; }
            set
            {
                if(_LessonId!=value){
                    _LessonId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="lesson_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Title;
        public string Title
        {
            get { return _Title; }
            set
            {
                if(_Title!=value){
                    _Title=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="title");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Lesson;
        public string Lesson
        {
            get { return _Lesson; }
            set
            {
                if(_Lesson!=value){
                    _Lesson=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="lesson");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime _DatePosted;
        public DateTime DatePosted
        {
            get { return _DatePosted; }
            set
            {
                if(_DatePosted!=value){
                    _DatePosted=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="date_posted");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime _DateModified;
        public DateTime DateModified
        {
            get { return _DateModified; }
            set
            {
                if(_DateModified!=value){
                    _DateModified=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="date_modified");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Files;
        public string Files
        {
            get { return _Files; }
            set
            {
                if(_Files!=value){
                    _Files=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="files");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<LessonBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the lessons_subjects table in the Eacademy Database.
    /// </summary>
    public partial class LessonsSubjectBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<LessonsSubjectBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<LessonsSubjectBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<LessonsSubjectBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(LessonsSubjectBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                LessonsSubjectBase item=new LessonsSubjectBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<LessonsSubjectBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public LessonsSubjectBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                LessonsSubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<LessonsSubjectBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public LessonsSubjectBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public LessonsSubjectBase(Expression<Func<LessonsSubjectBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<LessonsSubjectBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<LessonsSubjectBase> _repo;
            
            if(db.TestMode){
                LessonsSubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<LessonsSubjectBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<LessonsSubjectBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static LessonsSubjectBase SingleOrDefault(Expression<Func<LessonsSubjectBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            LessonsSubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static LessonsSubjectBase SingleOrDefault(Expression<Func<LessonsSubjectBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            LessonsSubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<LessonsSubjectBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<LessonsSubjectBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<LessonsSubjectBase> Find(Expression<Func<LessonsSubjectBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<LessonsSubjectBase> Find(Expression<Func<LessonsSubjectBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<LessonsSubjectBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<LessonsSubjectBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<LessonsSubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<LessonsSubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<LessonsSubjectBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<LessonsSubjectBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "LessonsSubjectId";
        }

        public object KeyValue()
        {
            return this.LessonsSubjectId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.LessonId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(LessonsSubjectBase)){
                LessonsSubjectBase compare=(LessonsSubjectBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.LessonsSubjectId;
        }
        
        public string DescriptorValue()
        {
                            return this.LessonId.ToString();
                    }

        public string DescriptorColumn() {
            return "LessonId";
        }
        public static string GetKeyColumn()
        {
            return "LessonsSubjectId";
        }        
        public static string GetDescriptorColumn()
        {
            return "LessonId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _LessonsSubjectId;
        public int LessonsSubjectId
        {
            get { return _LessonsSubjectId; }
            set
            {
                if(_LessonsSubjectId!=value){
                    _LessonsSubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="lessons_subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _LessonId;
        public int? LessonId
        {
            get { return _LessonId; }
            set
            {
                if(_LessonId!=value){
                    _LessonId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="lesson_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _SubjectId;
        public int? SubjectId
        {
            get { return _SubjectId; }
            set
            {
                if(_SubjectId!=value){
                    _SubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _MentorId;
        public int MentorId
        {
            get { return _MentorId; }
            set
            {
                if(_MentorId!=value){
                    _MentorId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="mentor_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<LessonsSubjectBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the mentors table in the Eacademy Database.
    /// </summary>
    public partial class MentorBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<MentorBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<MentorBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<MentorBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(MentorBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                MentorBase item=new MentorBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<MentorBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public MentorBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                MentorBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MentorBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public MentorBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public MentorBase(Expression<Func<MentorBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<MentorBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<MentorBase> _repo;
            
            if(db.TestMode){
                MentorBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MentorBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<MentorBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static MentorBase SingleOrDefault(Expression<Func<MentorBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            MentorBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static MentorBase SingleOrDefault(Expression<Func<MentorBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            MentorBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<MentorBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<MentorBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<MentorBase> Find(Expression<Func<MentorBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<MentorBase> Find(Expression<Func<MentorBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<MentorBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<MentorBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<MentorBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<MentorBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<MentorBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<MentorBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "MentorId";
        }

        public object KeyValue()
        {
            return this.MentorId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Firstname.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(MentorBase)){
                MentorBase compare=(MentorBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.MentorId;
        }
        
        public string DescriptorValue()
        {
                            return this.Firstname.ToString();
                    }

        public string DescriptorColumn() {
            return "Firstname";
        }
        public static string GetKeyColumn()
        {
            return "MentorId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Firstname";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _MentorId;
        public int MentorId
        {
            get { return _MentorId; }
            set
            {
                if(_MentorId!=value){
                    _MentorId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="mentor_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _UserId;
        public int? UserId
        {
            get { return _UserId; }
            set
            {
                if(_UserId!=value){
                    _UserId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Firstname;
        public string Firstname
        {
            get { return _Firstname; }
            set
            {
                if(_Firstname!=value){
                    _Firstname=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="firstname");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Middlename;
        public string Middlename
        {
            get { return _Middlename; }
            set
            {
                if(_Middlename!=value){
                    _Middlename=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="middlename");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Lastname;
        public string Lastname
        {
            get { return _Lastname; }
            set
            {
                if(_Lastname!=value){
                    _Lastname=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="lastname");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _EmployeeNumber;
        public string EmployeeNumber
        {
            get { return _EmployeeNumber; }
            set
            {
                if(_EmployeeNumber!=value){
                    _EmployeeNumber=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="employee_number");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<MentorBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the mentors_subjects table in the Eacademy Database.
    /// </summary>
    public partial class MentorsSubjectBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<MentorsSubjectBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<MentorsSubjectBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<MentorsSubjectBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(MentorsSubjectBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                MentorsSubjectBase item=new MentorsSubjectBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<MentorsSubjectBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public MentorsSubjectBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                MentorsSubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MentorsSubjectBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public MentorsSubjectBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public MentorsSubjectBase(Expression<Func<MentorsSubjectBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<MentorsSubjectBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<MentorsSubjectBase> _repo;
            
            if(db.TestMode){
                MentorsSubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MentorsSubjectBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<MentorsSubjectBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static MentorsSubjectBase SingleOrDefault(Expression<Func<MentorsSubjectBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            MentorsSubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static MentorsSubjectBase SingleOrDefault(Expression<Func<MentorsSubjectBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            MentorsSubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<MentorsSubjectBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<MentorsSubjectBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<MentorsSubjectBase> Find(Expression<Func<MentorsSubjectBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<MentorsSubjectBase> Find(Expression<Func<MentorsSubjectBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<MentorsSubjectBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<MentorsSubjectBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<MentorsSubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<MentorsSubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<MentorsSubjectBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<MentorsSubjectBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "MentorsSubjectId";
        }

        public object KeyValue()
        {
            return this.MentorsSubjectId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.MentorId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(MentorsSubjectBase)){
                MentorsSubjectBase compare=(MentorsSubjectBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.MentorsSubjectId;
        }
        
        public string DescriptorValue()
        {
                            return this.MentorId.ToString();
                    }

        public string DescriptorColumn() {
            return "MentorId";
        }
        public static string GetKeyColumn()
        {
            return "MentorsSubjectId";
        }        
        public static string GetDescriptorColumn()
        {
            return "MentorId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _MentorsSubjectId;
        public int MentorsSubjectId
        {
            get { return _MentorsSubjectId; }
            set
            {
                if(_MentorsSubjectId!=value){
                    _MentorsSubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="mentors_subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _MentorId;
        public int? MentorId
        {
            get { return _MentorId; }
            set
            {
                if(_MentorId!=value){
                    _MentorId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="mentor_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _SubjectId;
        public int? SubjectId
        {
            get { return _SubjectId; }
            set
            {
                if(_SubjectId!=value){
                    _SubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<MentorsSubjectBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the messages_conversations table in the Eacademy Database.
    /// </summary>
    public partial class MessagesConversationBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<MessagesConversationBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<MessagesConversationBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<MessagesConversationBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(MessagesConversationBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                MessagesConversationBase item=new MessagesConversationBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<MessagesConversationBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public MessagesConversationBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                MessagesConversationBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MessagesConversationBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public MessagesConversationBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public MessagesConversationBase(Expression<Func<MessagesConversationBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<MessagesConversationBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<MessagesConversationBase> _repo;
            
            if(db.TestMode){
                MessagesConversationBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MessagesConversationBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<MessagesConversationBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static MessagesConversationBase SingleOrDefault(Expression<Func<MessagesConversationBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            MessagesConversationBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static MessagesConversationBase SingleOrDefault(Expression<Func<MessagesConversationBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            MessagesConversationBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<MessagesConversationBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<MessagesConversationBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<MessagesConversationBase> Find(Expression<Func<MessagesConversationBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<MessagesConversationBase> Find(Expression<Func<MessagesConversationBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<MessagesConversationBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<MessagesConversationBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<MessagesConversationBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<MessagesConversationBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<MessagesConversationBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<MessagesConversationBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ConversationId";
        }

        public object KeyValue()
        {
            return this.ConversationId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Title.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(MessagesConversationBase)){
                MessagesConversationBase compare=(MessagesConversationBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ConversationId;
        }
        
        public string DescriptorValue()
        {
                            return this.Title.ToString();
                    }

        public string DescriptorColumn() {
            return "Title";
        }
        public static string GetKeyColumn()
        {
            return "ConversationId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Title";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ConversationId;
        public int ConversationId
        {
            get { return _ConversationId; }
            set
            {
                if(_ConversationId!=value){
                    _ConversationId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="conversation_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Title;
        public string Title
        {
            get { return _Title; }
            set
            {
                if(_Title!=value){
                    _Title=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="title");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _UserId;
        public int UserId
        {
            get { return _UserId; }
            set
            {
                if(_UserId!=value){
                    _UserId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Recipients;
        public int Recipients
        {
            get { return _Recipients; }
            set
            {
                if(_Recipients!=value){
                    _Recipients=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="recipients");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime _Modified;
        public DateTime Modified
        {
            get { return _Modified; }
            set
            {
                if(_Modified!=value){
                    _Modified=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="modified");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        bool _Locked;
        public bool Locked
        {
            get { return _Locked; }
            set
            {
                if(_Locked!=value){
                    _Locked=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="locked");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<MessagesConversationBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the messages_messages table in the Eacademy Database.
    /// </summary>
    public partial class MessagesMessageBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<MessagesMessageBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<MessagesMessageBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<MessagesMessageBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(MessagesMessageBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                MessagesMessageBase item=new MessagesMessageBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<MessagesMessageBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public MessagesMessageBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                MessagesMessageBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MessagesMessageBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public MessagesMessageBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public MessagesMessageBase(Expression<Func<MessagesMessageBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<MessagesMessageBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<MessagesMessageBase> _repo;
            
            if(db.TestMode){
                MessagesMessageBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MessagesMessageBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<MessagesMessageBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static MessagesMessageBase SingleOrDefault(Expression<Func<MessagesMessageBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            MessagesMessageBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static MessagesMessageBase SingleOrDefault(Expression<Func<MessagesMessageBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            MessagesMessageBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<MessagesMessageBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<MessagesMessageBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<MessagesMessageBase> Find(Expression<Func<MessagesMessageBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<MessagesMessageBase> Find(Expression<Func<MessagesMessageBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<MessagesMessageBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<MessagesMessageBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<MessagesMessageBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<MessagesMessageBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<MessagesMessageBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<MessagesMessageBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "MessageId";
        }

        public object KeyValue()
        {
            return this.MessageId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Title.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(MessagesMessageBase)){
                MessagesMessageBase compare=(MessagesMessageBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.MessageId;
        }
        
        public string DescriptorValue()
        {
                            return this.Title.ToString();
                    }

        public string DescriptorColumn() {
            return "Title";
        }
        public static string GetKeyColumn()
        {
            return "MessageId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Title";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _MessageId;
        public int MessageId
        {
            get { return _MessageId; }
            set
            {
                if(_MessageId!=value){
                    _MessageId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="message_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ConversationId;
        public int? ConversationId
        {
            get { return _ConversationId; }
            set
            {
                if(_ConversationId!=value){
                    _ConversationId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="conversation_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _UserId;
        public int? UserId
        {
            get { return _UserId; }
            set
            {
                if(_UserId!=value){
                    _UserId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Title;
        public string Title
        {
            get { return _Title; }
            set
            {
                if(_Title!=value){
                    _Title=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="title");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Body;
        public string Body
        {
            get { return _Body; }
            set
            {
                if(_Body!=value){
                    _Body=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="body");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _Date;
        public DateTime? Date
        {
            get { return _Date; }
            set
            {
                if(_Date!=value){
                    _Date=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="date");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _AttachmentType;
        public string AttachmentType
        {
            get { return _AttachmentType; }
            set
            {
                if(_AttachmentType!=value){
                    _AttachmentType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="attachment_type");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _AttachmentId;
        public int? AttachmentId
        {
            get { return _AttachmentId; }
            set
            {
                if(_AttachmentId!=value){
                    _AttachmentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="attachment_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<MessagesMessageBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the messages_recipients table in the Eacademy Database.
    /// </summary>
    public partial class MessagesRecipientBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<MessagesRecipientBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<MessagesRecipientBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<MessagesRecipientBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(MessagesRecipientBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                MessagesRecipientBase item=new MessagesRecipientBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<MessagesRecipientBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public MessagesRecipientBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                MessagesRecipientBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MessagesRecipientBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public MessagesRecipientBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public MessagesRecipientBase(Expression<Func<MessagesRecipientBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<MessagesRecipientBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<MessagesRecipientBase> _repo;
            
            if(db.TestMode){
                MessagesRecipientBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MessagesRecipientBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<MessagesRecipientBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static MessagesRecipientBase SingleOrDefault(Expression<Func<MessagesRecipientBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            MessagesRecipientBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static MessagesRecipientBase SingleOrDefault(Expression<Func<MessagesRecipientBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            MessagesRecipientBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<MessagesRecipientBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<MessagesRecipientBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<MessagesRecipientBase> Find(Expression<Func<MessagesRecipientBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<MessagesRecipientBase> Find(Expression<Func<MessagesRecipientBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<MessagesRecipientBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<MessagesRecipientBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<MessagesRecipientBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<MessagesRecipientBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<MessagesRecipientBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<MessagesRecipientBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "UserId";
        }

        public object KeyValue()
        {
            return this.UserId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ConversationId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(MessagesRecipientBase)){
                MessagesRecipientBase compare=(MessagesRecipientBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.UserId;
        }
        
        public string DescriptorValue()
        {
                            return this.ConversationId.ToString();
                    }

        public string DescriptorColumn() {
            return "ConversationId";
        }
        public static string GetKeyColumn()
        {
            return "UserId";
        }        
        public static string GetDescriptorColumn()
        {
            return "ConversationId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _UserId;
        public int UserId
        {
            get { return _UserId; }
            set
            {
                if(_UserId!=value){
                    _UserId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _ConversationId;
        public int ConversationId
        {
            get { return _ConversationId; }
            set
            {
                if(_ConversationId!=value){
                    _ConversationId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="conversation_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _InboxMessageId;
        public int? InboxMessageId
        {
            get { return _InboxMessageId; }
            set
            {
                if(_InboxMessageId!=value){
                    _InboxMessageId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="inbox_message_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _InboxUpdated;
        public DateTime? InboxUpdated
        {
            get { return _InboxUpdated; }
            set
            {
                if(_InboxUpdated!=value){
                    _InboxUpdated=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="inbox_updated");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        bool? _InboxRead;
        public bool? InboxRead
        {
            get { return _InboxRead; }
            set
            {
                if(_InboxRead!=value){
                    _InboxRead=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="inbox_read");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        bool? _InboxDeleted;
        public bool? InboxDeleted
        {
            get { return _InboxDeleted; }
            set
            {
                if(_InboxDeleted!=value){
                    _InboxDeleted=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="inbox_deleted");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _OutboxMessageId;
        public int? OutboxMessageId
        {
            get { return _OutboxMessageId; }
            set
            {
                if(_OutboxMessageId!=value){
                    _OutboxMessageId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="outbox_message_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _OutboxUpdated;
        public DateTime? OutboxUpdated
        {
            get { return _OutboxUpdated; }
            set
            {
                if(_OutboxUpdated!=value){
                    _OutboxUpdated=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="outbox_updated");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        bool? _OutboxDeleted;
        public bool? OutboxDeleted
        {
            get { return _OutboxDeleted; }
            set
            {
                if(_OutboxDeleted!=value){
                    _OutboxDeleted=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="outbox_deleted");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<MessagesRecipientBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the question_types table in the Eacademy Database.
    /// </summary>
    public partial class QuestionTypeBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<QuestionTypeBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<QuestionTypeBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<QuestionTypeBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(QuestionTypeBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                QuestionTypeBase item=new QuestionTypeBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<QuestionTypeBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public QuestionTypeBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                QuestionTypeBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuestionTypeBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public QuestionTypeBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public QuestionTypeBase(Expression<Func<QuestionTypeBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<QuestionTypeBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<QuestionTypeBase> _repo;
            
            if(db.TestMode){
                QuestionTypeBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuestionTypeBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<QuestionTypeBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static QuestionTypeBase SingleOrDefault(Expression<Func<QuestionTypeBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            QuestionTypeBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static QuestionTypeBase SingleOrDefault(Expression<Func<QuestionTypeBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            QuestionTypeBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<QuestionTypeBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<QuestionTypeBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<QuestionTypeBase> Find(Expression<Func<QuestionTypeBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<QuestionTypeBase> Find(Expression<Func<QuestionTypeBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<QuestionTypeBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<QuestionTypeBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<QuestionTypeBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<QuestionTypeBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<QuestionTypeBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<QuestionTypeBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "QuestionTypeId";
        }

        public object KeyValue()
        {
            return this.QuestionTypeId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.QuestionType.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(QuestionTypeBase)){
                QuestionTypeBase compare=(QuestionTypeBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.QuestionTypeId;
        }
        
        public string DescriptorValue()
        {
                            return this.QuestionType.ToString();
                    }

        public string DescriptorColumn() {
            return "QuestionType";
        }
        public static string GetKeyColumn()
        {
            return "QuestionTypeId";
        }        
        public static string GetDescriptorColumn()
        {
            return "QuestionType";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _QuestionTypeId;
        public int QuestionTypeId
        {
            get { return _QuestionTypeId; }
            set
            {
                if(_QuestionTypeId!=value){
                    _QuestionTypeId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="question_type_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _QuestionType;
        public string QuestionType
        {
            get { return _QuestionType; }
            set
            {
                if(_QuestionType!=value){
                    _QuestionType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="question_type");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<QuestionTypeBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the questions table in the Eacademy Database.
    /// </summary>
    public partial class QuestionBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<QuestionBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<QuestionBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<QuestionBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(QuestionBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                QuestionBase item=new QuestionBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<QuestionBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public QuestionBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                QuestionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuestionBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public QuestionBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public QuestionBase(Expression<Func<QuestionBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<QuestionBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<QuestionBase> _repo;
            
            if(db.TestMode){
                QuestionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuestionBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<QuestionBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static QuestionBase SingleOrDefault(Expression<Func<QuestionBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            QuestionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static QuestionBase SingleOrDefault(Expression<Func<QuestionBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            QuestionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<QuestionBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<QuestionBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<QuestionBase> Find(Expression<Func<QuestionBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<QuestionBase> Find(Expression<Func<QuestionBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<QuestionBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<QuestionBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<QuestionBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<QuestionBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<QuestionBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<QuestionBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "QuestionId";
        }

        public object KeyValue()
        {
            return this.QuestionId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Question.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(QuestionBase)){
                QuestionBase compare=(QuestionBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.QuestionId;
        }
        
        public string DescriptorValue()
        {
                            return this.Question.ToString();
                    }

        public string DescriptorColumn() {
            return "Question";
        }
        public static string GetKeyColumn()
        {
            return "QuestionId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Question";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _QuestionId;
        public int QuestionId
        {
            get { return _QuestionId; }
            set
            {
                if(_QuestionId!=value){
                    _QuestionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="question_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _QuizId;
        public int QuizId
        {
            get { return _QuizId; }
            set
            {
                if(_QuizId!=value){
                    _QuizId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="quiz_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Answer;
        public int Answer
        {
            get { return _Answer; }
            set
            {
                if(_Answer!=value){
                    _Answer=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="answer");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Question;
        public string Question
        {
            get { return _Question; }
            set
            {
                if(_Question!=value){
                    _Question=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="question");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _QuestionType;
        public int QuestionType
        {
            get { return _QuestionType; }
            set
            {
                if(_QuestionType!=value){
                    _QuestionType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="question_type");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _ChoiceIdAnswer;
        public int ChoiceIdAnswer
        {
            get { return _ChoiceIdAnswer; }
            set
            {
                if(_ChoiceIdAnswer!=value){
                    _ChoiceIdAnswer=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="choice_id_answer");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<QuestionBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the questions_answers table in the Eacademy Database.
    /// </summary>
    public partial class QuestionsAnswerBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<QuestionsAnswerBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<QuestionsAnswerBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<QuestionsAnswerBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(QuestionsAnswerBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                QuestionsAnswerBase item=new QuestionsAnswerBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<QuestionsAnswerBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public QuestionsAnswerBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                QuestionsAnswerBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuestionsAnswerBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public QuestionsAnswerBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public QuestionsAnswerBase(Expression<Func<QuestionsAnswerBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<QuestionsAnswerBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<QuestionsAnswerBase> _repo;
            
            if(db.TestMode){
                QuestionsAnswerBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuestionsAnswerBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<QuestionsAnswerBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static QuestionsAnswerBase SingleOrDefault(Expression<Func<QuestionsAnswerBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            QuestionsAnswerBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static QuestionsAnswerBase SingleOrDefault(Expression<Func<QuestionsAnswerBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            QuestionsAnswerBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<QuestionsAnswerBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<QuestionsAnswerBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<QuestionsAnswerBase> Find(Expression<Func<QuestionsAnswerBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<QuestionsAnswerBase> Find(Expression<Func<QuestionsAnswerBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<QuestionsAnswerBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<QuestionsAnswerBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<QuestionsAnswerBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<QuestionsAnswerBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<QuestionsAnswerBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<QuestionsAnswerBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "QuestionsAnswerId";
        }

        public object KeyValue()
        {
            return this.QuestionsAnswerId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.QuestionId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(QuestionsAnswerBase)){
                QuestionsAnswerBase compare=(QuestionsAnswerBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.QuestionsAnswerId;
        }
        
        public string DescriptorValue()
        {
                            return this.QuestionId.ToString();
                    }

        public string DescriptorColumn() {
            return "QuestionId";
        }
        public static string GetKeyColumn()
        {
            return "QuestionsAnswerId";
        }        
        public static string GetDescriptorColumn()
        {
            return "QuestionId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _QuestionsAnswerId;
        public int QuestionsAnswerId
        {
            get { return _QuestionsAnswerId; }
            set
            {
                if(_QuestionsAnswerId!=value){
                    _QuestionsAnswerId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="questions_answer_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _QuestionId;
        public int QuestionId
        {
            get { return _QuestionId; }
            set
            {
                if(_QuestionId!=value){
                    _QuestionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="question_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _ChoiceAnswerId;
        public int ChoiceAnswerId
        {
            get { return _ChoiceAnswerId; }
            set
            {
                if(_ChoiceAnswerId!=value){
                    _ChoiceAnswerId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="choice_answer_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _StudentId;
        public int StudentId
        {
            get { return _StudentId; }
            set
            {
                if(_StudentId!=value){
                    _StudentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="student_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _QuizStudentId;
        public int QuizStudentId
        {
            get { return _QuizStudentId; }
            set
            {
                if(_QuizStudentId!=value){
                    _QuizStudentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="quiz_student_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<QuestionsAnswerBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the quiz table in the Eacademy Database.
    /// </summary>
    public partial class QuizBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<QuizBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<QuizBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<QuizBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(QuizBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                QuizBase item=new QuizBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<QuizBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public QuizBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                QuizBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuizBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public QuizBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public QuizBase(Expression<Func<QuizBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<QuizBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<QuizBase> _repo;
            
            if(db.TestMode){
                QuizBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuizBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<QuizBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static QuizBase SingleOrDefault(Expression<Func<QuizBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            QuizBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static QuizBase SingleOrDefault(Expression<Func<QuizBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            QuizBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<QuizBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<QuizBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<QuizBase> Find(Expression<Func<QuizBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<QuizBase> Find(Expression<Func<QuizBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<QuizBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<QuizBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<QuizBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<QuizBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<QuizBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<QuizBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "QuizId";
        }

        public object KeyValue()
        {
            return this.QuizId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Description.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(QuizBase)){
                QuizBase compare=(QuizBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.QuizId;
        }
        
        public string DescriptorValue()
        {
                            return this.Description.ToString();
                    }

        public string DescriptorColumn() {
            return "Description";
        }
        public static string GetKeyColumn()
        {
            return "QuizId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Description";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _QuizId;
        public int QuizId
        {
            get { return _QuizId; }
            set
            {
                if(_QuizId!=value){
                    _QuizId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="quiz_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _TimeLimit;
        public int TimeLimit
        {
            get { return _TimeLimit; }
            set
            {
                if(_TimeLimit!=value){
                    _TimeLimit=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="time_limit");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _TotalItems;
        public int TotalItems
        {
            get { return _TotalItems; }
            set
            {
                if(_TotalItems!=value){
                    _TotalItems=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="total_items");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Description;
        public string Description
        {
            get { return _Description; }
            set
            {
                if(_Description!=value){
                    _Description=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="description");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _PassingScore;
        public int PassingScore
        {
            get { return _PassingScore; }
            set
            {
                if(_PassingScore!=value){
                    _PassingScore=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="passing_score");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime _DatePosted;
        public DateTime DatePosted
        {
            get { return _DatePosted; }
            set
            {
                if(_DatePosted!=value){
                    _DatePosted=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="date_posted");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<QuizBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the quiz_students table in the Eacademy Database.
    /// </summary>
    public partial class QuizStudentBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<QuizStudentBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<QuizStudentBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<QuizStudentBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(QuizStudentBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                QuizStudentBase item=new QuizStudentBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<QuizStudentBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public QuizStudentBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                QuizStudentBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuizStudentBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public QuizStudentBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public QuizStudentBase(Expression<Func<QuizStudentBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<QuizStudentBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<QuizStudentBase> _repo;
            
            if(db.TestMode){
                QuizStudentBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuizStudentBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<QuizStudentBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static QuizStudentBase SingleOrDefault(Expression<Func<QuizStudentBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            QuizStudentBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static QuizStudentBase SingleOrDefault(Expression<Func<QuizStudentBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            QuizStudentBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<QuizStudentBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<QuizStudentBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<QuizStudentBase> Find(Expression<Func<QuizStudentBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<QuizStudentBase> Find(Expression<Func<QuizStudentBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<QuizStudentBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<QuizStudentBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<QuizStudentBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<QuizStudentBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<QuizStudentBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<QuizStudentBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "QuizStudentId";
        }

        public object KeyValue()
        {
            return this.QuizStudentId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.RandomKey.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(QuizStudentBase)){
                QuizStudentBase compare=(QuizStudentBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.QuizStudentId;
        }
        
        public string DescriptorValue()
        {
                            return this.RandomKey.ToString();
                    }

        public string DescriptorColumn() {
            return "RandomKey";
        }
        public static string GetKeyColumn()
        {
            return "QuizStudentId";
        }        
        public static string GetDescriptorColumn()
        {
            return "RandomKey";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _QuizStudentId;
        public int QuizStudentId
        {
            get { return _QuizStudentId; }
            set
            {
                if(_QuizStudentId!=value){
                    _QuizStudentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="quiz_student_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _MaxQuestionId;
        public int MaxQuestionId
        {
            get { return _MaxQuestionId; }
            set
            {
                if(_MaxQuestionId!=value){
                    _MaxQuestionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="max_question_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _RandomKey;
        public string RandomKey
        {
            get { return _RandomKey; }
            set
            {
                if(_RandomKey!=value){
                    _RandomKey=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="random_key");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Score;
        public int Score
        {
            get { return _Score; }
            set
            {
                if(_Score!=value){
                    _Score=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="score");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _TotalItems;
        public int TotalItems
        {
            get { return _TotalItems; }
            set
            {
                if(_TotalItems!=value){
                    _TotalItems=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="total_items");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime _DateStarted;
        public DateTime DateStarted
        {
            get { return _DateStarted; }
            set
            {
                if(_DateStarted!=value){
                    _DateStarted=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="date_started");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime _DateEnd;
        public DateTime DateEnd
        {
            get { return _DateEnd; }
            set
            {
                if(_DateEnd!=value){
                    _DateEnd=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="date_end");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _QuizId;
        public int QuizId
        {
            get { return _QuizId; }
            set
            {
                if(_QuizId!=value){
                    _QuizId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="quiz_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _StudentId;
        public int StudentId
        {
            get { return _StudentId; }
            set
            {
                if(_StudentId!=value){
                    _StudentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="student_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _Done;
        public int Done
        {
            get { return _Done; }
            set
            {
                if(_Done!=value){
                    _Done=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="done");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<QuizStudentBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the quiz_subjects table in the Eacademy Database.
    /// </summary>
    public partial class QuizSubjectBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<QuizSubjectBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<QuizSubjectBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<QuizSubjectBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(QuizSubjectBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                QuizSubjectBase item=new QuizSubjectBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<QuizSubjectBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public QuizSubjectBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                QuizSubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuizSubjectBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public QuizSubjectBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public QuizSubjectBase(Expression<Func<QuizSubjectBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<QuizSubjectBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<QuizSubjectBase> _repo;
            
            if(db.TestMode){
                QuizSubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<QuizSubjectBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<QuizSubjectBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static QuizSubjectBase SingleOrDefault(Expression<Func<QuizSubjectBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            QuizSubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static QuizSubjectBase SingleOrDefault(Expression<Func<QuizSubjectBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            QuizSubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<QuizSubjectBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<QuizSubjectBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<QuizSubjectBase> Find(Expression<Func<QuizSubjectBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<QuizSubjectBase> Find(Expression<Func<QuizSubjectBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<QuizSubjectBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<QuizSubjectBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<QuizSubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<QuizSubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<QuizSubjectBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<QuizSubjectBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "QuizSubjectId";
        }

        public object KeyValue()
        {
            return this.QuizSubjectId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.QuizId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(QuizSubjectBase)){
                QuizSubjectBase compare=(QuizSubjectBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.QuizSubjectId;
        }
        
        public string DescriptorValue()
        {
                            return this.QuizId.ToString();
                    }

        public string DescriptorColumn() {
            return "QuizId";
        }
        public static string GetKeyColumn()
        {
            return "QuizSubjectId";
        }        
        public static string GetDescriptorColumn()
        {
            return "QuizId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _QuizSubjectId;
        public int QuizSubjectId
        {
            get { return _QuizSubjectId; }
            set
            {
                if(_QuizSubjectId!=value){
                    _QuizSubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="quiz_subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _QuizId;
        public int QuizId
        {
            get { return _QuizId; }
            set
            {
                if(_QuizId!=value){
                    _QuizId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="quiz_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _SubjectId;
        public int? SubjectId
        {
            get { return _SubjectId; }
            set
            {
                if(_SubjectId!=value){
                    _SubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _MentorId;
        public int MentorId
        {
            get { return _MentorId; }
            set
            {
                if(_MentorId!=value){
                    _MentorId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="mentor_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<QuizSubjectBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the sections table in the Eacademy Database.
    /// </summary>
    public partial class SectionBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<SectionBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<SectionBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<SectionBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(SectionBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                SectionBase item=new SectionBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<SectionBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public SectionBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                SectionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<SectionBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public SectionBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public SectionBase(Expression<Func<SectionBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<SectionBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<SectionBase> _repo;
            
            if(db.TestMode){
                SectionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<SectionBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<SectionBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static SectionBase SingleOrDefault(Expression<Func<SectionBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            SectionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static SectionBase SingleOrDefault(Expression<Func<SectionBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            SectionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<SectionBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<SectionBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<SectionBase> Find(Expression<Func<SectionBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<SectionBase> Find(Expression<Func<SectionBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<SectionBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<SectionBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<SectionBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<SectionBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<SectionBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<SectionBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "SectionsId";
        }

        public object KeyValue()
        {
            return this.SectionsId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.SectionName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(SectionBase)){
                SectionBase compare=(SectionBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.SectionsId;
        }
        
        public string DescriptorValue()
        {
                            return this.SectionName.ToString();
                    }

        public string DescriptorColumn() {
            return "SectionName";
        }
        public static string GetKeyColumn()
        {
            return "SectionsId";
        }        
        public static string GetDescriptorColumn()
        {
            return "SectionName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _SectionsId;
        public int SectionsId
        {
            get { return _SectionsId; }
            set
            {
                if(_SectionsId!=value){
                    _SectionsId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="sections_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SectionName;
        public string SectionName
        {
            get { return _SectionName; }
            set
            {
                if(_SectionName!=value){
                    _SectionName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="section_name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<SectionBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the sections_subjects table in the Eacademy Database.
    /// </summary>
    public partial class SectionsSubjectBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<SectionsSubjectBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<SectionsSubjectBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<SectionsSubjectBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(SectionsSubjectBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                SectionsSubjectBase item=new SectionsSubjectBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<SectionsSubjectBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public SectionsSubjectBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                SectionsSubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<SectionsSubjectBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public SectionsSubjectBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public SectionsSubjectBase(Expression<Func<SectionsSubjectBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<SectionsSubjectBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<SectionsSubjectBase> _repo;
            
            if(db.TestMode){
                SectionsSubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<SectionsSubjectBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<SectionsSubjectBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static SectionsSubjectBase SingleOrDefault(Expression<Func<SectionsSubjectBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            SectionsSubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static SectionsSubjectBase SingleOrDefault(Expression<Func<SectionsSubjectBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            SectionsSubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<SectionsSubjectBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<SectionsSubjectBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<SectionsSubjectBase> Find(Expression<Func<SectionsSubjectBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<SectionsSubjectBase> Find(Expression<Func<SectionsSubjectBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<SectionsSubjectBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<SectionsSubjectBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<SectionsSubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<SectionsSubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<SectionsSubjectBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<SectionsSubjectBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "SectionsSubjectId";
        }

        public object KeyValue()
        {
            return this.SectionsSubjectId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.SectionId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(SectionsSubjectBase)){
                SectionsSubjectBase compare=(SectionsSubjectBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.SectionsSubjectId;
        }
        
        public string DescriptorValue()
        {
                            return this.SectionId.ToString();
                    }

        public string DescriptorColumn() {
            return "SectionId";
        }
        public static string GetKeyColumn()
        {
            return "SectionsSubjectId";
        }        
        public static string GetDescriptorColumn()
        {
            return "SectionId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _SectionsSubjectId;
        public int SectionsSubjectId
        {
            get { return _SectionsSubjectId; }
            set
            {
                if(_SectionsSubjectId!=value){
                    _SectionsSubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="sections_subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _SectionId;
        public int SectionId
        {
            get { return _SectionId; }
            set
            {
                if(_SectionId!=value){
                    _SectionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="section_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _SubjectId;
        public int SubjectId
        {
            get { return _SubjectId; }
            set
            {
                if(_SubjectId!=value){
                    _SubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<SectionsSubjectBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the students table in the Eacademy Database.
    /// </summary>
    public partial class StudentBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<StudentBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<StudentBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<StudentBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(StudentBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                StudentBase item=new StudentBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<StudentBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public StudentBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                StudentBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<StudentBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public StudentBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public StudentBase(Expression<Func<StudentBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<StudentBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<StudentBase> _repo;
            
            if(db.TestMode){
                StudentBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<StudentBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<StudentBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static StudentBase SingleOrDefault(Expression<Func<StudentBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            StudentBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static StudentBase SingleOrDefault(Expression<Func<StudentBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            StudentBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<StudentBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<StudentBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<StudentBase> Find(Expression<Func<StudentBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<StudentBase> Find(Expression<Func<StudentBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<StudentBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<StudentBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<StudentBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<StudentBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<StudentBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<StudentBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "StudentId";
        }

        public object KeyValue()
        {
            return this.StudentId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<long>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Firstname.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(StudentBase)){
                StudentBase compare=(StudentBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public string DescriptorValue()
        {
                            return this.Firstname.ToString();
                    }

        public string DescriptorColumn() {
            return "Firstname";
        }
        public static string GetKeyColumn()
        {
            return "StudentId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Firstname";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        long _StudentId;
        public long StudentId
        {
            get { return _StudentId; }
            set
            {
                if(_StudentId!=value){
                    _StudentId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="student_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Firstname;
        public string Firstname
        {
            get { return _Firstname; }
            set
            {
                if(_Firstname!=value){
                    _Firstname=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="firstname");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Middlename;
        public string Middlename
        {
            get { return _Middlename; }
            set
            {
                if(_Middlename!=value){
                    _Middlename=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="middlename");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Lastname;
        public string Lastname
        {
            get { return _Lastname; }
            set
            {
                if(_Lastname!=value){
                    _Lastname=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="lastname");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        long? _UserId;
        public long? UserId
        {
            get { return _UserId; }
            set
            {
                if(_UserId!=value){
                    _UserId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _StudentNumber;
        public string StudentNumber
        {
            get { return _StudentNumber; }
            set
            {
                if(_StudentNumber!=value){
                    _StudentNumber=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="student_number");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<StudentBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the subjects table in the Eacademy Database.
    /// </summary>
    public partial class SubjectBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<SubjectBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<SubjectBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<SubjectBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(SubjectBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                SubjectBase item=new SubjectBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<SubjectBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public SubjectBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                SubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<SubjectBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public SubjectBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public SubjectBase(Expression<Func<SubjectBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<SubjectBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<SubjectBase> _repo;
            
            if(db.TestMode){
                SubjectBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<SubjectBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<SubjectBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static SubjectBase SingleOrDefault(Expression<Func<SubjectBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            SubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static SubjectBase SingleOrDefault(Expression<Func<SubjectBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            SubjectBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<SubjectBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<SubjectBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<SubjectBase> Find(Expression<Func<SubjectBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<SubjectBase> Find(Expression<Func<SubjectBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<SubjectBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<SubjectBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<SubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<SubjectBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<SubjectBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<SubjectBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "SubjectId";
        }

        public object KeyValue()
        {
            return this.SubjectId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.SubjectCode.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(SubjectBase)){
                SubjectBase compare=(SubjectBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.SubjectId;
        }
        
        public string DescriptorValue()
        {
                            return this.SubjectCode.ToString();
                    }

        public string DescriptorColumn() {
            return "SubjectCode";
        }
        public static string GetKeyColumn()
        {
            return "SubjectId";
        }        
        public static string GetDescriptorColumn()
        {
            return "SubjectCode";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _SubjectId;
        public int SubjectId
        {
            get { return _SubjectId; }
            set
            {
                if(_SubjectId!=value){
                    _SubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SubjectCode;
        public string SubjectCode
        {
            get { return _SubjectCode; }
            set
            {
                if(_SubjectCode!=value){
                    _SubjectCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_code");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SubjectName;
        public string SubjectName
        {
            get { return _SubjectName; }
            set
            {
                if(_SubjectName!=value){
                    _SubjectName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<SubjectBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the subjects_enrolled table in the Eacademy Database.
    /// </summary>
    public partial class SubjectsEnrolledBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<SubjectsEnrolledBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<SubjectsEnrolledBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<SubjectsEnrolledBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(SubjectsEnrolledBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                SubjectsEnrolledBase item=new SubjectsEnrolledBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<SubjectsEnrolledBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public SubjectsEnrolledBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                SubjectsEnrolledBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<SubjectsEnrolledBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public SubjectsEnrolledBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public SubjectsEnrolledBase(Expression<Func<SubjectsEnrolledBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<SubjectsEnrolledBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<SubjectsEnrolledBase> _repo;
            
            if(db.TestMode){
                SubjectsEnrolledBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<SubjectsEnrolledBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<SubjectsEnrolledBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static SubjectsEnrolledBase SingleOrDefault(Expression<Func<SubjectsEnrolledBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            SubjectsEnrolledBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static SubjectsEnrolledBase SingleOrDefault(Expression<Func<SubjectsEnrolledBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            SubjectsEnrolledBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<SubjectsEnrolledBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<SubjectsEnrolledBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<SubjectsEnrolledBase> Find(Expression<Func<SubjectsEnrolledBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<SubjectsEnrolledBase> Find(Expression<Func<SubjectsEnrolledBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<SubjectsEnrolledBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<SubjectsEnrolledBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<SubjectsEnrolledBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<SubjectsEnrolledBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<SubjectsEnrolledBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<SubjectsEnrolledBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "SubjectEnrolledId";
        }

        public object KeyValue()
        {
            return this.SubjectEnrolledId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.UserId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(SubjectsEnrolledBase)){
                SubjectsEnrolledBase compare=(SubjectsEnrolledBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.SubjectEnrolledId;
        }
        
        public string DescriptorValue()
        {
                            return this.UserId.ToString();
                    }

        public string DescriptorColumn() {
            return "UserId";
        }
        public static string GetKeyColumn()
        {
            return "SubjectEnrolledId";
        }        
        public static string GetDescriptorColumn()
        {
            return "UserId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _SubjectEnrolledId;
        public int SubjectEnrolledId
        {
            get { return _SubjectEnrolledId; }
            set
            {
                if(_SubjectEnrolledId!=value){
                    _SubjectEnrolledId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_enrolled_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _UserId;
        public int? UserId
        {
            get { return _UserId; }
            set
            {
                if(_UserId!=value){
                    _UserId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _SubjectId;
        public int? SubjectId
        {
            get { return _SubjectId; }
            set
            {
                if(_SubjectId!=value){
                    _SubjectId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="subject_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _MentorId;
        public int MentorId
        {
            get { return _MentorId; }
            set
            {
                if(_MentorId!=value){
                    _MentorId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="mentor_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        bool _Enabled;
        public bool Enabled
        {
            get { return _Enabled; }
            set
            {
                if(_Enabled!=value){
                    _Enabled=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="enabled");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<SubjectsEnrolledBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the users table in the Eacademy Database.
    /// </summary>
    public partial class UserBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<UserBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<UserBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<UserBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(UserBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                UserBase item=new UserBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<UserBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public UserBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                UserBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<UserBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public UserBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public UserBase(Expression<Func<UserBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<UserBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<UserBase> _repo;
            
            if(db.TestMode){
                UserBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<UserBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<UserBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static UserBase SingleOrDefault(Expression<Func<UserBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            UserBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static UserBase SingleOrDefault(Expression<Func<UserBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            UserBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<UserBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<UserBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<UserBase> Find(Expression<Func<UserBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<UserBase> Find(Expression<Func<UserBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<UserBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<UserBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<UserBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<UserBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<UserBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<UserBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "UserId";
        }

        public object KeyValue()
        {
            return this.UserId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<long>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Username.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(UserBase)){
                UserBase compare=(UserBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public string DescriptorValue()
        {
                            return this.Username.ToString();
                    }

        public string DescriptorColumn() {
            return "Username";
        }
        public static string GetKeyColumn()
        {
            return "UserId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Username";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        long _UserId;
        public long UserId
        {
            get { return _UserId; }
            set
            {
                if(_UserId!=value){
                    _UserId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _UserTypeId;
        public int UserTypeId
        {
            get { return _UserTypeId; }
            set
            {
                if(_UserTypeId!=value){
                    _UserTypeId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_type_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Username;
        public string Username
        {
            get { return _Username; }
            set
            {
                if(_Username!=value){
                    _Username=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="username");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Password;
        public string Password
        {
            get { return _Password; }
            set
            {
                if(_Password!=value){
                    _Password=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="password");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PasswordSalt;
        public string PasswordSalt
        {
            get { return _PasswordSalt; }
            set
            {
                if(_PasswordSalt!=value){
                    _PasswordSalt=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="password_salt");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UserPrimaryEmail;
        public string UserPrimaryEmail
        {
            get { return _UserPrimaryEmail; }
            set
            {
                if(_UserPrimaryEmail!=value){
                    _UserPrimaryEmail=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_primary_email");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UserAlternateEmail;
        public string UserAlternateEmail
        {
            get { return _UserAlternateEmail; }
            set
            {
                if(_UserAlternateEmail!=value){
                    _UserAlternateEmail=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_alternate_email");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _UserLastVisit;
        public DateTime? UserLastVisit
        {
            get { return _UserLastVisit; }
            set
            {
                if(_UserLastVisit!=value){
                    _UserLastVisit=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_last_visit");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _UserLoginAttempts;
        public int? UserLoginAttempts
        {
            get { return _UserLoginAttempts; }
            set
            {
                if(_UserLoginAttempts!=value){
                    _UserLoginAttempts=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_login_attempts");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _LastUserActivity;
        public DateTime? LastUserActivity
        {
            get { return _LastUserActivity; }
            set
            {
                if(_LastUserActivity!=value){
                    _LastUserActivity=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="last_user_activity");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _TotalSubjectCount;
        public int? TotalSubjectCount
        {
            get { return _TotalSubjectCount; }
            set
            {
                if(_TotalSubjectCount!=value){
                    _TotalSubjectCount=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="total_subject_count");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _AvailableSubjectCount;
        public int? AvailableSubjectCount
        {
            get { return _AvailableSubjectCount; }
            set
            {
                if(_AvailableSubjectCount!=value){
                    _AvailableSubjectCount=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="available_subject_count");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ThumbImageName;
        public string ThumbImageName
        {
            get { return _ThumbImageName; }
            set
            {
                if(_ThumbImageName!=value){
                    _ThumbImageName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="thumb_image_name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<UserBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the users_permission table in the Eacademy Database.
    /// </summary>
    public partial class UsersPermissionBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<UsersPermissionBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<UsersPermissionBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<UsersPermissionBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(UsersPermissionBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                UsersPermissionBase item=new UsersPermissionBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<UsersPermissionBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public UsersPermissionBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                UsersPermissionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<UsersPermissionBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public UsersPermissionBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public UsersPermissionBase(Expression<Func<UsersPermissionBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<UsersPermissionBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<UsersPermissionBase> _repo;
            
            if(db.TestMode){
                UsersPermissionBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<UsersPermissionBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<UsersPermissionBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static UsersPermissionBase SingleOrDefault(Expression<Func<UsersPermissionBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            UsersPermissionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static UsersPermissionBase SingleOrDefault(Expression<Func<UsersPermissionBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            UsersPermissionBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<UsersPermissionBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<UsersPermissionBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<UsersPermissionBase> Find(Expression<Func<UsersPermissionBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<UsersPermissionBase> Find(Expression<Func<UsersPermissionBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<UsersPermissionBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<UsersPermissionBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<UsersPermissionBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<UsersPermissionBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<UsersPermissionBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<UsersPermissionBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "UserpermissionId";
        }

        public object KeyValue()
        {
            return this.UserpermissionId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.UsertypeId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(UsersPermissionBase)){
                UsersPermissionBase compare=(UsersPermissionBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.UserpermissionId;
        }
        
        public string DescriptorValue()
        {
                            return this.UsertypeId.ToString();
                    }

        public string DescriptorColumn() {
            return "UsertypeId";
        }
        public static string GetKeyColumn()
        {
            return "UserpermissionId";
        }        
        public static string GetDescriptorColumn()
        {
            return "UsertypeId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _UserpermissionId;
        public int UserpermissionId
        {
            get { return _UserpermissionId; }
            set
            {
                if(_UserpermissionId!=value){
                    _UserpermissionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="userpermission_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _UsertypeId;
        public int? UsertypeId
        {
            get { return _UsertypeId; }
            set
            {
                if(_UsertypeId!=value){
                    _UsertypeId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="usertype_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _AccountpermissionId;
        public int? AccountpermissionId
        {
            get { return _AccountpermissionId; }
            set
            {
                if(_AccountpermissionId!=value){
                    _AccountpermissionId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="accountpermission_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int _SetPermission;
        public int SetPermission
        {
            get { return _SetPermission; }
            set
            {
                if(_SetPermission!=value){
                    _SetPermission=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="set_permission");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<UsersPermissionBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the users_type table in the Eacademy Database.
    /// </summary>
    public partial class UsersTypeBase: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<UsersTypeBase> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<UsersTypeBase>(new Eacademy.Domain.Entities.EacademyDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<UsersTypeBase> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(UsersTypeBase item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                UsersTypeBase item=new UsersTypeBase();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<UsersTypeBase> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Eacademy.Domain.Entities.EacademyDB _db;
        public UsersTypeBase(string connectionString, string providerName) {

            _db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                UsersTypeBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<UsersTypeBase>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public UsersTypeBase(){
             _db=new Eacademy.Domain.Entities.EacademyDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public UsersTypeBase(Expression<Func<UsersTypeBase, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<UsersTypeBase> GetRepo(string connectionString, string providerName){
            Eacademy.Domain.Entities.EacademyDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Eacademy.Domain.Entities.EacademyDB();
            }else{
                db=new Eacademy.Domain.Entities.EacademyDB(connectionString, providerName);
            }
            IRepository<UsersTypeBase> _repo;
            
            if(db.TestMode){
                UsersTypeBase.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<UsersTypeBase>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<UsersTypeBase> GetRepo(){
            return GetRepo("","");
        }
        
        public static UsersTypeBase SingleOrDefault(Expression<Func<UsersTypeBase, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            UsersTypeBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static UsersTypeBase SingleOrDefault(Expression<Func<UsersTypeBase, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            UsersTypeBase single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<UsersTypeBase, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<UsersTypeBase, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<UsersTypeBase> Find(Expression<Func<UsersTypeBase, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<UsersTypeBase> Find(Expression<Func<UsersTypeBase, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<UsersTypeBase> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<UsersTypeBase> All() {
            return GetRepo().GetAll();
        }
        
        public static SubSonic.Schema.PagedList<UsersTypeBase> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static SubSonic.Schema.PagedList<UsersTypeBase> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static SubSonic.Schema.PagedList<UsersTypeBase> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static SubSonic.Schema.PagedList<UsersTypeBase> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "UsertypeId";
        }

        public object KeyValue()
        {
            return this.UsertypeId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.UserType.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(UsersTypeBase)){
                UsersTypeBase compare=(UsersTypeBase)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.UsertypeId;
        }
        
        public string DescriptorValue()
        {
                            return this.UserType.ToString();
                    }

        public string DescriptorColumn() {
            return "UserType";
        }
        public static string GetKeyColumn()
        {
            return "UsertypeId";
        }        
        public static string GetDescriptorColumn()
        {
            return "UserType";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _UsertypeId;
        public int UsertypeId
        {
            get { return _UsertypeId; }
            set
            {
                if(_UsertypeId!=value){
                    _UsertypeId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="usertype_id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UserType;
        public string UserType
        {
            get { return _UserType; }
            set
            {
                if(_UserType!=value){
                    _UserType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="user_type");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<UsersTypeBase, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
}
